import Vue from 'vue'
import { getCompany, getProject } from '@/api/themeChoose/themeChoose'
import { getPostsChangeLog } from '@/api/messageSend'

export const themeChoose = {
  data(){
    return{
      company:[],
      project:[],
      projectPast:[],
      companyParams:{},
      projectParams:{},
      onlyCompany:0 // 值为1：只请求公司，不请求项目；值为0：请求公司和项目；父组件设置即可更改值
    }
  },
  created(){
    const userInfo = Vue.getAcloudProvider().getUserInfo()
    this.getCompanyList({orgIds:[userInfo.orgId], tagCodes:['BC']})
  },
  methods: {
    getCompanyList(params){
      getCompany(params).then(res => {
        if(res.status === 200){
          const data = res.data;
          this.company = res.data;
          if(this.onlyCompany === 1){
            return false
          }else {
            const ordIdArray=data.map(function(item) {
              return item.orgId
            })
            this.getProjectList({query:{branchComIds:ordIdArray}})
          }
        }else {
          this.$message.error('err:', res.statusText);
        }
      })
    },
    getProjectList(params){
      getProject(params).then(res => {
        if(res.code === 200){
          this.project = res.data
        }else {
          this.$message.error('err:', res.message)
        }
      })
      //查已归档的项目。note：上面的祖传代码，可能有用，先不动他的
      let data = JSON.parse(JSON.stringify(params))
      data.auditStatus = 'PAST'
      getProject(data).then(res => {
        if(res.code === 200){
          this.projectPast = res.data
        }else {
          this.$message.error('err:', res.message)
        }
      })
    },
    handleChangeCompany(params) {
      this.companyParams=params
      if(this.onlyCompany == 0){
        return
      }else {
        this.getProjectList({query:{branchComIds:[params.key]}})
      }

    },
    handleChangeProject(params) {
      this.projectParams=params
    },

  },

}
export const postChangeLog = {
  data(){
    return{}
  },
  methods: {
    /**
     * 岗位变更日志
     * 获取一周内岗位变更数量
     */
    handleGetPostsChangeLog(){
      let param = {
        Authorization: Vue.getAcloudProvider().getAccessToken(),
      }
      getPostsChangeLog(param).then((res) => {
        let count = res.data
        this.$store.commit('messageInfo/SET_TOREADPOSTSCHANGELOGCOUNT', count)
      })
    },
  }
}

export const mixin ={
  themeChoose,
  postChangeLog
}
export default mixin